package ro.ubb.web.books;

import java.io.IOException;
import java.io.Writer;
import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ro.ubb.dataaccess.entities.Book;
import ro.ubb.executor.Executor;

public class GetAllBooks extends HttpServlet {

	private static final long serialVersionUID = 1L;


	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Writer out = response.getWriter();
		try{
		final InitialContext ic = new InitialContext();
        final Executor executor = (Executor) ic.lookup("java:global/bsee-ear/bsee-web-0.0.1-SNAPSHOT/Executor");
		List<Book> books = executor.getAllBooks();
		out.append("Result:\n");
		if (books.isEmpty()) {
			out.append("Empty!");
		}
		for (Book book : books) {
			out.append(book.toString() + "\n");
		}
		}catch(NamingException ne){
        	ne.printStackTrace();
        }

	}
}
